package com.example.myspringboot.caffeine.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author zheng
 */
@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();

        //针对配置信息进行缓存
        caffeineCacheManager.registerCustomCache("IZUUL", Caffeine.newBuilder()
                .maximumSize(10000).build());
        caffeineCacheManager.registerCustomCache("IZUUL", Caffeine.newBuilder()
                .expireAfterWrite(5000, TimeUnit.MILLISECONDS)
                .maximumSize(10000).build());
        return caffeineCacheManager;
    }
}
